package com.trace.wells.charactertracker;

import java.util.ArrayList;
import java.util.zip.Inflater;

import org.w3c.dom.Text;

import com.trace.wells.charactertracker.WeaponListCustomAdapter.ViewHolder;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ItemAdapter extends BaseExpandableListAdapter {

	private LayoutInflater inflater;
	private ArrayList<Item>[] items;
	private Context context;
	private Character character;

	public ItemAdapter(Context context, ArrayList<Item>[] items) {
		this.context = context;
		this.items = items;
		//		this.character = character;
	}

	@Override
	public Object getChild(int arg0, int arg1) {
		Item item = items[arg0].get(arg1);
		return item;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		final Item i = (Item) getChild(groupPosition, childPosition);
		ViewObj genericHolder = null;
		Object holder = null;
		if (convertView == null) {

			genericHolder = prepareHolder(i.getItemTypeInt(), i.isPermanent());

			convertView = genericHolder.getView();

			convertView.setTag(genericHolder.getObject());

			//			View returnedView = getCorrespondingView(i.getItemTypeInt(), i.isPermanent());
		} else {
			switch (i.getItemTypeInt()) {
			case ConstantHelper.ITEM_TYPE_POTION:

				Potion pot = (Potion) convertView.getTag();

				pot.potionName.setText(i.getName());

				//				Item potion = i;
				//				View tempView = genericHolder.getView();
				break;
			case ConstantHelper.ITEM_TYPE_MISC:
				if (i.isPermanent()) {
					MiscPerm miscPerm = (MiscPerm) convertView.getTag();

					miscPerm.miscName.setText(i.getName());
					miscPerm.itemDesc.setText(i.getDescription());
				} else {
					MiscTemp miscTemp = (MiscTemp) convertView.getTag();

					miscTemp.itemName.setText(i.getName());
					miscTemp.progress.setMax(i.getMaxUses());
					miscTemp.progress.setProgress(i.getUsesRemaining());
					miscTemp.useButton.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							i.decrementUsesRemaining();
						}
					});
				}
				break;
			case ConstantHelper.ITEM_TYPE_RING:
				if (i.isPermanent()) {
					RingPerm ringPerm = (RingPerm) convertView.getTag();

					ringPerm.ringName.setText(i.getName());

					//TODO: START OFF HERE

					//ringPerm.casterLevel.setText(i.get
				} else {
					holder = (RingTemp) convertView.getTag();
				}
				break;
			case ConstantHelper.ITEM_TYPE_STAFF:
				holder = (Staff) convertView.getTag();
				break;
			case ConstantHelper.ITEM_TYPE_SCROLL:
				holder = (Scroll) convertView.getTag();
				break;
			case ConstantHelper.ITEM_TYPE_WAND:
				holder = (Wand) convertView.getTag();
				break;
			default:
				break;
			}
		}

		return convertView;
	}

	private ViewObj prepareHolder(int itemTypeInt, boolean isPermanent) {
		Object returnHolder = null;
		View tempConvertView = null;
		switch (itemTypeInt) {
		case ConstantHelper.ITEM_TYPE_POTION:
			tempConvertView = inflater.inflate(R.layout.item_row_potion, null);
			Potion potionHolder = new Potion();
			potionHolder.potionName = (TextView) tempConvertView.findViewById(R.id.itemPotionCustomSpellNameTxt);
			returnHolder = potionHolder;
			break;
		case ConstantHelper.ITEM_TYPE_MISC:
			if (isPermanent) {
				tempConvertView = inflater.inflate(R.layout.item_row_misc_perm, null);
				MiscPerm miscHolder = new MiscPerm();
				miscHolder.miscName = (TextView) tempConvertView.findViewById(R.id.itemRowMiscPermName);
				miscHolder.itemDesc = (TextView) tempConvertView.findViewById(R.id.itemRowMiscPermDesc);
				returnHolder = miscHolder;
			} else {
				tempConvertView = inflater.inflate(R.layout.item_row_misc_temp, null);
				MiscTemp miscHolder = new MiscTemp();
				miscHolder.itemName = (TextView) tempConvertView.findViewById(R.id.itemRowMiscTempName);
				miscHolder.progress = (ProgressBar) tempConvertView.findViewById(R.id.itemRowMiscTempProgress);
				miscHolder.useButton = (Button) tempConvertView.findViewById(R.id.itemRowMiscTempButton);
				returnHolder = miscHolder;
			}
			break;
		case ConstantHelper.ITEM_TYPE_RING:
			if (isPermanent) {
				tempConvertView = inflater.inflate(R.layout.item_row_ring_perm, null);
				RingPerm ringHolder = new RingPerm();
				ringHolder.ringName = (TextView) tempConvertView.findViewById(R.id.itemRowRingPermName);
				ringHolder.casterLevel = (TextView) tempConvertView.findViewById(R.id.itemRowRingPermCasterLevel);
				ringHolder.value = (TextView) tempConvertView.findViewById(R.id.itemRowRingPermValue);
				returnHolder = ringHolder;
			} else {
				tempConvertView = inflater.inflate(R.layout.item_row_ring_temp, null);
				RingTemp ringHolder = new RingTemp();
				ringHolder.ringName = (TextView) tempConvertView.findViewById(R.id.itemRowRingTempName);
				ringHolder.casterLevel = (TextView) tempConvertView.findViewById(R.id.itemRowRingTempCasterLevel);
				ringHolder.progress = (ProgressBar) tempConvertView.findViewById(R.id.itemRowRingTempProgress);
				ringHolder.useButton = (Button) tempConvertView.findViewById(R.id.itemRowRingTempButton);
				returnHolder = ringHolder;
			}
			break;
		case ConstantHelper.ITEM_TYPE_STAFF:
			tempConvertView = inflater.inflate(R.layout.item_row_staff, null);
			Staff staffHolder = new Staff();
			staffHolder.staffName = (TextView) tempConvertView.findViewById(R.id.itemRowStaffName);
			staffHolder.progress = (ProgressBar) tempConvertView.findViewById(R.id.itemRowStaffProgress);
			staffHolder.spellInfo = (Button) tempConvertView.findViewById(R.id.itemRowStaffInfoButton);
			staffHolder.useButton = (Button) tempConvertView.findViewById(R.id.itemRowStaffUseButton);
			returnHolder = staffHolder;
			break;

		case ConstantHelper.ITEM_TYPE_SCROLL:
			tempConvertView = inflater.inflate(R.layout.item_scroll_custom, null);
			Scroll scrollHolder = new Scroll();
			scrollHolder.scrollName = (TextView) tempConvertView.findViewById(R.id.itemRowScrollName);
			scrollHolder.spellInfo = (Button) tempConvertView.findViewById(R.id.itemRowScrollInfoButton);
			scrollHolder.useButton = (Button) tempConvertView.findViewById(R.id.itemRowScrollUseButton);
			returnHolder = scrollHolder;
			break;

		case ConstantHelper.ITEM_TYPE_WAND:
			tempConvertView = inflater.inflate(R.layout.item_wand_custom, null);
			Wand wandHolder = new Wand();
			wandHolder.wandName = (TextView) tempConvertView.findViewById(R.id.itemRowWandTempName);
			wandHolder.casterLevel = (TextView) tempConvertView.findViewById(R.id.itemRowWandTempCasterLevel);
			wandHolder.progress = (ProgressBar) tempConvertView.findViewById(R.id.itemRowWandTempProgress);
			wandHolder.useButton = (Button) tempConvertView.findViewById(R.id.itemRowWandTempButton);
			returnHolder = wandHolder;
			break;
		default:
			break;
		}

		ViewObj vo = new ViewObj(tempConvertView, returnHolder);

		return vo;
	}

	public class ViewObj {
		/**
		 * @return the view
		 */
		public View getView() {
			return view;
		}

		/**
		 * @param view
		 *            the view to set
		 */
		public void setView(View view) {
			this.view = view;
		}

		/**
		 * @return the object
		 */
		public Object getObject() {
			return object;
		}

		/**
		 * @param object
		 *            the object to set
		 */
		public void setObject(Object object) {
			this.object = object;
		}

		public ViewObj(View view, Object object) {
			super();
			this.view = view;
			this.object = object;
		}

		View view;
		Object object;
	}

	/*
	 * 	public static final int ITEM_TYPE_POTION = 0;
		public static final int ITEM_TYPE_RING = 1;
		public static final int ITEM_TYPE_WAND = 2;
		public static final int ITEM_TYPE_STAFF = 3;
		public static final int ITEM_TYPE_SCROLL = 4;
		public static final int ITEM_TYPE_ROD = 5;
		public static final int ITEM_TYPE_MISC = 6;
	 */
	private View getCorrespondingView(int itemType, boolean isPermanent) {
		return null;
		//		switch () {
		//		case ConstantHelper.ITEM_TYPE_POTION:
		//
		//			break;
		//
		//		default:
		//			break;
		//		}
		//		return null;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return items[groupPosition].size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return items[groupPosition];
	}

	@Override
	public int getGroupCount() {
		//Gets the number of "active" groups
		int groupCount = 0;
		for (int i = 0; i < items.length; i++) {
			if (items[i].size() > 0) {
				groupCount++;
			}
		}
		return groupCount;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return false;
	}

	private class Potion {
		TextView potionName;
	}

	private class MiscPerm {
		TextView miscName;
		TextView itemDesc;
	}

	private class MiscTemp {
		TextView itemName;
		ProgressBar progress;
		Button useButton;
	}

	private class RingTemp {
		TextView ringName;
		TextView casterLevel;
		ProgressBar progress;
		Button useButton;
	}

	private class RingPerm {
		TextView ringName;
		TextView casterLevel;
		TextView value;
	}

	private class Staff {
		TextView staffName;
		ProgressBar progress;
		Button spellInfo;
		Button useButton;
	}

	private class Scroll {
		TextView scrollName;
		Button spellInfo;
		Button useButton;
	}

	private class Wand {
		TextView wandName;
		TextView casterLevel;
		ProgressBar progress;
		Button useButton;
	}

	///////////////////////////////////////////

	private class MiscCustom {
		EditText itemName;
		CheckBox permanentCB;
		EditText charges;
		EditText casterLevel;
		EditText description;
		ImageButton descMic;
		Button addButton;
	}

	private class PotionCustom {
		EditText spellName;
		ImageButton spellNameMic;
		EditText spellDescription;
		ImageButton spellDescMic;
		Button addButton;
	}

	private class PotionStandard {
		AutoCompleteTextView spellName;
		Button addButton;
	}

	private class RingCustom {
		EditText ringName;
		CheckBox permanentCB;
		EditText charges;
		EditText casterLevel;
		EditText description;
		ImageButton descMic;
		Button addButton;
	}

	private class RingStandard {
		AutoCompleteTextView ringName;
		TextView description;
		Button addButton;
	}

	private class RodCustom {
		EditText rodName;
		CheckBox permanentCB;
		EditText charges;
		EditText description;
		ImageButton descMic;
		Button addButton;
	}

	private class ScrollCustom {
		EditText scrollName;
		ImageButton scrollNameMic;
		AutoCompleteTextView spellName;
		Button addSpell;
		TextView scrollDesc;
		Button addButton;
	}

	private class StaffCustom {
		EditText staffName;
		ImageButton staffNameMic;
		EditText charges;
		EditText casterLeve;
		AutoCompleteTextView spellName;
		EditText chargeCost;
		Button addSpell;
		TextView staffDesc;
		Button addButton;
	}

	private class StaffStandard {
		AutoCompleteTextView staffName;
		TextView staffDesc;
		Button addButton;
	}

	private class WandCustom {
		EditText wandName;
		ImageButton wandNameMic;
		EditText charges;
		AutoCompleteTextView spellName;
		Button addButton;
	}

}
